<?php
class GraphHelper extends Helper {
	
	var $data = array();
	var $return = array();
	var $user_id = false;
	var $labels = array('time', 'distance', 'calories'); /* procent added later on */
	var $total = array(); // Needed to calulate procent
	
	
	/* $key = all, total, type, heartrate, ability */
	function stats($data, $key = 'all', $user_id = null) {
		$this->data = $data;
		$this->user_id = $user_id;
		
		$this->return = array('Labels' => $this->labels);
		//, 'Total' => array(), 'Types' => array(), 'HRZones' => array()
		
		switch ($key) {
			case 'total':
				$this->return['Total'] = $this->_total();
				break;
			
			case 'type':
				$this->return['Total'] = $this->total = $this->_total();
				$this->return['Type'] = $this->_type();
				break;
				
			case 'heartrate':
				$this->return['Total'] = $this->total = $this->_total();
				$this->return['Heartrate'] = $this->_heartrate();
				break;

			case 'ability':
				$this->return['Total'] = $this->total = $this->_total();
				$this->return['Ability'] = $this->_ability();
				break;
			default:
				$this->return['Total'] = $this->total = $this->_total();
				$this->return['Type'] = $this->_type();
				$this->return['Heartrate'] = $this->_heartrate();
				$this->return['Ability'] = $this->_ability();
				break;
		}
		$this->return['Labels'][] = 'procent';
		//debug($this->return);
		return $this->return;
	}
	
	function _total() {
		$return = array();
		foreach($this->labels as $label) {
			$return[] = $this->_calulate($label, $this->data);
		}
		$return[] = array('procent'=>100);
		return $return;
	}
	
	function _type() {
		$return = array();
		// Get the users workout types
		App::import("Model", "Type"); //ClassRegistry::init("Workout");
    	$model = new Type();
		if($this->user_id) {
			$model->Behaviors->Ownable->settings['Type']['enabled'] = false;
			$types = $model->find('list', array('conditions'=>array('user_id'=>$this->user_id)));
		} else {
			$types = $model->find('list', array());
		}
		// Calulate types
		foreach($types as $key => $value) {
			foreach($this->labels as $label) {
				$return[$value][] = $this->_calulate($label, $this->data, $key);
			}
			$procent = 0;
			if($return[$value][0]['time'] > 0) {
				$procent = round(($return[$value][0]['time'] / $this->total[0]['time']) * 100, 1);
			}
				
			$return[$value][] = array('procent'=>$procent);
		}
		return $return;
	}
	
	function _heartrate() {
		$return = array();
		$default_zone = Configure::read('Workout.defaultZone');
		$top_zone = Configure::read('Workout.topZone');
		$zones = array();
		for($j = $default_zone; $j <= $top_zone; $j++) {
			$zones[$j] = __('HR Zone', true) . ' ' . $j;
		}
		$upper_zone_time = 0;
		$default_zone_key = "";
		foreach($zones as $key => $value) {
			foreach($this->labels as $label) {
				$return[$value][] = $this->_calulate($label, $this->data, null, $key);
			}
			if($key != $default_zone) {
				$upper_zone_time += $return[$value][0]['time'];
			} else {
				$default_zone_key = $value;
			}
			$procent = 0;
			if($return[$value][0]['time'] > 0) {
				$procent = round(($return[$value][0]['time'] / $this->total[0]['time']) * 100, 1);
			}
			$return[$value][] = array('procent'=>$procent);
		}
		$upper_zone_time = $this->total[0]['time'] - $upper_zone_time;
		// Fix time in zone 1, add rest from other zones
		$return[$default_zone_key][0]['time'] = $upper_zone_time;
		$return[$default_zone_key][3]['procent'] = 0;
		if($return[$default_zone_key][0]['time']>0) {
			$return[$default_zone_key][3]['procent'] = round(($return[$default_zone_key][0]['time'] / $this->total[0]['time']) * 100, 1);
		}
		
		return $return;
	}
	
	function _ability() {
		$return = array();
		// Get the users workout types
		App::import("Model", "Ability"); //ClassRegistry::init("Workout");
    	$model = new Ability();
		if($this->user_id) {
			$model->Behaviors->Ownable->settings['Ability']['enabled'] = false;
			$ability = $model->find('list', array('conditions'=>array('user_id'=>$this->user_id)));
		} else {
			$ability = $model->find('list', array());
		}
		// Calulate types
		foreach($ability as $key => $value) {
			foreach($this->labels as $label) {
				$return[$value][] = $this->_calulate($label, $this->data, null, null, $key);
			}
			$procent = 0;
			if($return[$value][0]['time'] > 0) {
				$procent = round(($return[$value][0]['time'] / $this->total[0]['time']) * 100, 1);
			}
				
			$return[$value][] = array('procent'=>$procent);
		}
		return $return;
	}
	
	function _calulate($key, $data, $type_id = null, $zone_id = null, $ability_id = null) {
		
		if($type_id) {
			$data_filtered = array();
			for($i = 0; $i < count($data); $i++) {
				if(Set::matches(array("type_id=$type_id"), $data[$i]['Template'])) {
					//debug($data[$i]);
					$data_filtered[]= $data[$i];
				}
			}
			$data = $data_filtered;
		}
		if($ability_id) {
			$data_filtered = array();
			for($i = 0; $i < count($data); $i++) {
				if(Set::matches(array("ability_id=$ability_id"), $data[$i]['Template'])) {
					//debug($data[$i]);
					$data_filtered[]= $data[$i];
				}
			}
			$data = $data_filtered;
		}
		
		if($zone_id) {
			$data_filtered = array();
			for($i = 0; $i < count($data); $i++) {
				if(Set::matches(array("power_zone=$zone_id"), $data[$i]['Template'])) {
					//debug($data[$i]);
					if($data[$i]['Template']['power_zone'] != Configure::read('Workout.defaultZone') ) {
						$data[$i]['Workout']['time_total'] = $data[$i]['Template']['time_in_power_zone'];
					}
					
					$data_filtered[]= $data[$i];
				}
			}
			$data = $data_filtered;
		}
		switch ($key) {
			case 'time':
				$return = 0;
				foreach($data as $workout) {
					//debug($return);
					$return += $workout['Workout']['time_total'];
				}
				return array('time'=>$return);
			case 'distance':
				$return = 0;
				foreach($data as $workout) {
					//debug($return);
					$return += $workout['Workout']['distance'];
				}
				return array('distance'=>$return);
			case 'calories':
				$return = 0;
				foreach($data as $workout) {
					//debug($return);
					$return += $workout['Workout']['kcal'];
				}
				return array('calories'=>$return);
			case 'procent':
				return array('procent'=>'100');	
			default:
				return null;
		}
	}

}

?>